package com.next.aappublicapp;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.widget.ShareActionProvider;
import com.google.analytics.tracking.android.EasyTracker;
import com.next.aappublicapp.util.ImageUtil;

public class BaseFragmentActivty extends SherlockFragmentActivity{

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.Theme_Sherlock_Light); //Used for theme switching in samples
		super.onCreate(savedInstanceState);
		EasyTracker.getInstance().activityStart(this);
		
	}
	
    @Override
    public void onStop() {
        super.onStop();
        EasyTracker.getInstance().activityStop(this);
    }
    /**
     * Event Handling for Individual menu item selected
     * Identify single menu item by it's id
     * */
    @Override
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
        // Inflate your menu.
        getSupportMenuInflater().inflate(R.menu.main, menu);

        // Set file with share history to the provider and set the share intent.
        MenuItem actionItem = menu.findItem(R.id.menu_item_share_action_provider_action_bar);
        ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
        actionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
        // Note that you can set/change the intent any time,
        // say when the user has selected an image.
        Log.i("Ravi", "menu.toString()="+menu.toString()+","+actionItem.getItemId());
        Log.i("Ravi", "actionItem.getTitle()="+actionItem.getTitle());
        Log.i("Ravi", "actionItem.hasSubMenu()="+actionItem.hasSubMenu());
        
        actionProvider.setShareIntent(createShareIntent());

        //XXX: For now, ShareActionProviders must be displayed on the action bar
        // Set file with share history to the provider and set the share intent.
        //MenuItem overflowItem = menu.findItem(R.id.menu_item_share_action_provider_overflow);
        //ShareActionProvider overflowProvider =
        //    (ShareActionProvider) overflowItem.getActionProvider();
        //overflowProvider.setShareHistoryFileName(
        //    ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
        // Note that you can set/change the intent any time,
        // say when the user has selected an image.
        //overflowProvider.setShareIntent(createShareIntent());

        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	Log.e("Ravi", "Menu selected is "+item.getTitle());
    	super.onOptionsItemSelected(item);
        return false;
    }
    /**
     * Creates a sharing {@link Intent}.
     *
     * @return The sharing intent.
     */
    protected Intent createShareIntent() {
        Intent shareIntent = createShareIntent("Install AAP android app from http://bit.ly/fbappinstall and get realtime updates on news/video/facebook", "Install AAP android app from http://bit.ly/fbappinstall");
        return shareIntent;
    }
    
    protected Intent createShareIntent(String text,String title) {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
    	try{
            Uri uri = Uri.fromFile(getFileStreamPath(AapApplication.SHARED_FILE_NAME));
            shareIntent = createShareIntent(uri, text, title);
    	}catch(Exception ex){
    		ex.printStackTrace();
    	}
        return shareIntent;
    }
    protected Intent createShareIntent(ImageView imageView,String text,String title) {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
    	try{
    		ImageUtil.copyPrivateRawResourceToPubliclyAccessibleFile(this, imageView);
            Uri uri = Uri.fromFile(getFileStreamPath(AapApplication.SHARED_FILE_NAME));
            shareIntent = createShareIntent(uri, text, title);
    	}catch(Exception ex){
    		ex.printStackTrace();
    	}
        return shareIntent;
    }
    protected Intent createShareIntent(Uri uri,String text,String title) {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
    	try{
            shareIntent.setType("*/*");
            shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
            shareIntent.putExtra(Intent.EXTRA_TEXT, text);
            shareIntent.putExtra(Intent.EXTRA_TITLE, title);
            shareIntent.putExtra(Intent.EXTRA_SUBJECT, title);
    	}catch(Exception ex){
    		ex.printStackTrace();
    	}
        return shareIntent;
    }
    
    
    /*
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
         Boolean returnValue = MenuUtil.onOptionsItemSelected(item, this);
         if(returnValue == null){
        	 return super.onOptionsItemSelected(item); 
         }
         return returnValue;
    }   
    */
}
